<template>
  <div>
    <navCom></navCom>
    <el-form ref="form" label-position="left" :model="form" label-width="50px">
      <el-form-item label="标题">
        <el-input v-model="form.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="内容">
        <div class="editor">
          <div ref="toolbar" class="toolbar"></div>
          <div ref="editor" class="text"></div>
        </div>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="type" value-key="value" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">发布</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import navCom from "../components/publicComponent/nav";
import E from "wangeditor";
export default {
  data() {
    return {
      form: {
        title: ""
      },
      options: [],
      type: ""
    };
  },
  mounted() {
    this.seteditor();
    this.getType()
  },
  methods: {
    seteditor() {
      this.editor = new E(this.$refs.toolbar, this.$refs.editor);
      this.editor.customConfig.uploadImgShowBase64 = false; // base 64 存储图片
      this.editor.customConfig.uploadImgServer =
        "http://otp.cdinfotech.top/file/upload_images"; // 配置服务器端地址
      this.editor.customConfig.uploadImgHeaders = {}; // 自定义 header
      this.editor.customConfig.uploadFileName = "file"; // 后端接受上传文件的参数名
      this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024; // 将图片大小限制为 2M
      this.editor.customConfig.uploadImgMaxLength = 6; // 限制一次最多上传 3 张图片
      this.editor.customConfig.uploadImgTimeout = 3 * 60 * 1000; // 设置超时时间

      // 配置菜单
      this.editor.customConfig.menus = [
        "head", // 标题
        "bold", // 粗体
        "fontSize", // 字号
        "fontName", // 字体
        "italic", // 斜体
        "underline", // 下划线
        "strikeThrough", // 删除线
        "foreColor", // 文字颜色
        "backColor", // 背景颜色
        "link", // 插入链接
        "list", // 列表
        "justify", // 对齐方式
        "quote", // 引用
        "emoticon", // 表情
        "image", // 插入图片
        "table", // 表格
        "code", // 插入代码
        "undo", // 撤销
        "redo", // 重复
        "fullscreen" // 全屏
      ];

      this.editor.customConfig.uploadImgHooks = {
        fail: (xhr, editor, result) => {
          // 插入图片失败回调
        },
        success: (xhr, editor, result) => {
          // 图片上传成功回调
        },
        timeout: (xhr, editor) => {
          // 网络超时的回调
        },
        error: (xhr, editor) => {
          // 图片上传错误的回调
        },
        customInsert: (insertImg, result, editor) => {
          // 图片上传成功，插入图片的回调
          //result为上传图片成功的时候返回的数据，这里我打印了一下发现后台返回的是data：[{url:"路径的形式"},...]
          // console.log(result.data[0].url)
          //insertImg()为插入图片的函数
          //循环插入图片
          // for (let i = 0; i < 1; i++) {
          // console.log(result)
          let url = "http://otp.cdinfotech.top" + result.url;
          insertImg(url);
          // }
        }
      };
      this.editor.customConfig.onchange = html => {
        this.info_ = html; // 绑定当前逐渐地值
        this.$emit("change", this.info_); // 将内容同步到父组件中
      };
      // 创建富文本编辑器
      this.editor.create();
    },
    save(){
      if (this.form.title == "") {
        this.$message(
            {
              message: "标题不能为空",
              type: "error"
            },
            1000
          );
        return
      }
      if (this.editor.txt.html() == "<p><br></p>") {
        this.$message(
            {
              message: "内容不能为空",
              type: "error"
            },
            1000
          );
        return
      }
      if (this.type == "") {
        this.$message(
            {
              message: "请选择类型",
              type: "error"
            },
            1000
          );
        return
      }
      let data = {
        user:JSON.parse(localStorage.getItem('userInfo')).userName,
        acticleType:this.type,
        name:this.form.title,
        content:this.editor.txt.html()
      }
      this.$ajax.post('/acticle/addActicle/',data).then((res) => {
        if (res.status == 1) {
         this.$message(
            {
              message: res.message,
              type: "success"
            },
            1000
          );
        } else {
          this.$message(
            {
              message: res.message,
              type: "error"
            },
            1000
          );
        }
      })
    },
    getType(){
      this.$ajax.post('/acticle/acticleTypeList/',{}).then((res) => {
        if (res.status == 1) {
          res.data.forEach(el => {
            Object.keys(el.fields).forEach(item => {
              this.options.push({
                value: el.pk,
                label: el.fields[item]
              })
            })
          })
        } else {
          this.$message(
            {
              message: res.message,
              type: "error"
            },
            1000
          );
        }
      })
    }
  },
  components: {
    navCom
  }
};
</script>
<style scoped>
.w-e-text-container {
  border: 1px solid #ccc;
  height: 450px;
  z-index: 0 !important;
}
</style>